package com.springmvc.shangguigu.interceptors;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * FirstInterceptor--SpringMVC-test
 *
 * @author: Jovi
 * @createTime: 2018-09-14 14:49
 **/
public class SecondInterceptor implements HandlerInterceptor {

    /**
     * 该方法在目标方法之前被调用，若返回值为true则继续调用后续拦截器和目标方法，
     * 若返回值为false则不会再调用后续拦截器和目标方法
     *
     * 可以考虑做权限，日志，事务
     * @param request
     * @param response
     * @param handler
     * @return
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        System.out.println("[SecondInterceptor] preHandle");
        return true;
    }

    /**
     * 调用目标方法之后，但渲染视图之前被调用
     *
     * 可以对请求域中的属性或视图做出修改
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("[SecondInterceptor] postHandle");
    }

    /**
     * 渲染视图之后被调用
     *
     * 释放资源
     * @param request
     * @param response
     * @param handler
     * @param ex
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        System.out.println("[SecondInterceptor] afterCompletion");
    }
}
